<template>
  <nav class="navbar">
    <div class="nav-container">
      <div class="nav-logo">
        <i class="fas fa-brain"></i>
        <span>智索真源</span>
      </div>
      <ul class="nav-menu" :class="{ active: mobileMenuOpen }">
        <li class="nav-item">
          <a href="/" class="nav-link" @click="navigateToHome">首页</a>
        </li>
        <li class="nav-item">
          <a href="/#about" class="nav-link" @click="navigateToSection('about')">关于我们</a>
        </li>
        <li class="nav-item">
          <a href="/#products" class="nav-link" @click="navigateToSection('products')">产品服务</a>
        </li>
        <li class="nav-item">
          <a href="/#solutions" class="nav-link" @click="navigateToSection('solutions')">解决方案</a>
        </li>
        <li class="nav-item">
          <a href="/#cases" class="nav-link" @click="navigateToSection('cases')">案例展示</a>
        </li>
        <li class="nav-item">
          <a href="/#team" class="nav-link" @click="navigateToSection('team')">团队介绍</a>
        </li>
        <li class="nav-item">
          <a href="/#contact" class="nav-link" @click="navigateToSection('contact')">联系我们</a>
        </li>
      </ul>
      
      <!-- 身份切换下拉菜单 -->
      <div class="identity-switcher" v-if="currentIdentity">
        <div class="identity-dropdown" :class="{ active: dropdownOpen }" @click="toggleDropdown">
          <div class="current-identity">
            <i :class="getIdentityIcon(currentIdentity)"></i>
            <span>{{ getIdentityLabel(currentIdentity) }}</span>
            <i class="fas fa-chevron-down dropdown-arrow"></i>
          </div>
          <div class="dropdown-menu" v-show="dropdownOpen">
            <div class="dropdown-item" 
                 v-for="identity in availableIdentities" 
                 :key="identity.value"
                 @click="switchIdentity(identity.value)"
                 :class="{ active: identity.value === currentIdentity }">
              <i :class="identity.icon"></i>
              <span>{{ identity.label }}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="nav-toggle" @click="toggleMobileMenu">
        <span class="bar"></span>
        <span class="bar"></span>
        <span class="bar"></span>
      </div>
    </div>
  </nav>
</template>

<script>
export default {
  name: 'NavbarComponent',
  data() {
    return {
      mobileMenuOpen: false,
      dropdownOpen: false,
      currentIdentity: null,
      availableIdentities: [
        { value: 'investor', label: '投资者', icon: 'fas fa-chart-line' },
        { value: 'enterprise', label: '企业方', icon: 'fas fa-building' },
        { value: 'student', label: '大学生', icon: 'fas fa-graduation-cap' }
      ]
    }
  },
  mounted() {
    // 获取当前用户身份
    this.currentIdentity = localStorage.getItem('userIdentity')
    
    // 点击外部关闭下拉菜单
    document.addEventListener('click', this.handleClickOutside)
  },
  beforeUnmount() {
    document.removeEventListener('click', this.handleClickOutside)
  },
  methods: {
    toggleMobileMenu() {
      this.mobileMenuOpen = !this.mobileMenuOpen
    },
    closeMobileMenu() {
      this.mobileMenuOpen = false
    },
    toggleDropdown() {
      this.dropdownOpen = !this.dropdownOpen
    },
    handleClickOutside(event) {
      if (!this.$el.contains(event.target)) {
        this.dropdownOpen = false
      }
    },
    getIdentityIcon(identity) {
      const identityMap = {
        'investor': 'fas fa-chart-line',
        'enterprise': 'fas fa-building',
        'student': 'fas fa-graduation-cap'
      }
      return identityMap[identity] || 'fas fa-user'
    },
    getIdentityLabel(identity) {
      const labelMap = {
        'investor': '投资者',
        'enterprise': '企业方',
        'student': '大学生'
      }
      return labelMap[identity] || '未知身份'
    },
    switchIdentity(newIdentity) {
      if (newIdentity !== this.currentIdentity) {
        this.currentIdentity = newIdentity
        localStorage.setItem('userIdentity', newIdentity)
        
        // 触发父组件更新
        this.$emit('identity-changed', newIdentity)
        
        // 关闭下拉菜单
        this.dropdownOpen = false
        
        // 滚动到顶部
        window.scrollTo({ top: 0, behavior: 'smooth' })
      }
    },
    navigateToHome(event) {
      event.preventDefault()
      this.closeMobileMenu()
      if (this.$route.path !== '/') {
        this.$router.push('/')
      } else {
        // 如果已经在首页，滚动到顶部
        window.scrollTo({ top: 0, behavior: 'smooth' })
      }
    },
    navigateToSection(sectionId) {
      event.preventDefault()
      this.closeMobileMenu()
      
      if (this.$route.path !== '/') {
        // 如果不在首页，先跳转到首页
        this.$router.push('/').then(() => {
          // 等待页面加载完成后滚动到指定section
          setTimeout(() => {
            this.scrollToSection(sectionId)
          }, 100)
        })
      } else {
        // 如果已经在首页，直接滚动到指定section
        this.scrollToSection(sectionId)
      }
    },
    scrollToSection(sectionId) {
      // 如果是案例展示且用户身份是企业，跳转到成功案例
      if (sectionId === 'cases' && this.currentIdentity === 'enterprise') {
        const element = document.getElementById('success-cases')
        if (element) {
          element.scrollIntoView({ 
            behavior: 'smooth',
            block: 'start'
          })
          return
        }
      }
      
      const element = document.getElementById(sectionId)
      if (element) {
        element.scrollIntoView({ 
          behavior: 'smooth',
          block: 'start'
        })
      }
    }
  }
}
</script>

<style scoped>
.navbar {
  position: fixed;
  top: 0;
  width: 100%;
  background: rgba(15, 15, 35, 0.9);
  backdrop-filter: blur(20px);
  z-index: 1000;
  transition: all 0.3s ease;
  box-shadow: 0 2px 20px rgba(0, 212, 255, 0.2);
  border-bottom: 1px solid rgba(0, 212, 255, 0.3);
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 70px;
}

.nav-logo {
  display: flex;
  align-items: center;
  font-size: 1.5rem;
  font-weight: 700;
  background: linear-gradient(45deg, #00d4ff, #5b73e8);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  position: relative;
}

.nav-logo i {
  margin-right: 10px;
  font-size: 1.8rem;
  color: #00d4ff;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

.nav-menu {
  display: flex;
  list-style: none;
  gap: 2rem;
}

.nav-link {
  text-decoration: none;
  color: rgba(255, 255, 255, 0.8);
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
  padding: 8px 16px;
  border-radius: 20px;
}

.nav-link:hover {
  color: #00d4ff;
  background: rgba(0, 212, 255, 0.1);
  box-shadow: 0 0 20px rgba(0, 212, 255, 0.3);
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 2px;
  background: linear-gradient(45deg, #00d4ff, #5b73e8);
  transition: width 0.3s ease;
  border-radius: 1px;
}

.nav-link:hover::after {
  width: 80%;
}

.nav-toggle {
  display: none;
  flex-direction: column;
  cursor: pointer;
}

.bar {
  width: 25px;
  height: 3px;
  background: white;
  margin: 3px 0;
  transition: 0.3s;
  border-radius: 2px;
}

/* 身份切换器样式 */
.identity-switcher {
  position: relative;
  margin-left: 1rem;
}

.identity-dropdown {
  position: relative;
  cursor: pointer;
}

.current-identity {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: rgba(0, 212, 255, 0.1);
  border: 1px solid rgba(0, 212, 255, 0.3);
  border-radius: 25px;
  color: white;
  font-size: 0.9rem;
  font-weight: 500;
  transition: all 0.3s ease;
  min-width: 120px;
}

.current-identity:hover {
  background: rgba(0, 212, 255, 0.2);
  border-color: rgba(0, 212, 255, 0.5);
  box-shadow: 0 0 20px rgba(0, 212, 255, 0.3);
}

.current-identity i:first-child {
  color: #00d4ff;
  font-size: 1rem;
}

.dropdown-arrow {
  font-size: 0.8rem;
  transition: transform 0.3s ease;
  margin-left: auto;
}

.identity-dropdown.active .dropdown-arrow {
  transform: rotate(180deg);
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: rgba(15, 15, 35, 0.95);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(0, 212, 255, 0.3);
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 212, 255, 0.2);
  z-index: 1000;
  min-width: 150px;
  overflow: hidden;
  animation: dropdownSlide 0.3s ease;
}

@keyframes dropdownSlide {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  padding: 0.8rem 1rem;
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.9rem;
  transition: all 0.3s ease;
  cursor: pointer;
}

.dropdown-item:hover {
  background: rgba(0, 212, 255, 0.1);
  color: white;
}

.dropdown-item.active {
  background: rgba(0, 212, 255, 0.2);
  color: #00d4ff;
}

.dropdown-item i {
  font-size: 1rem;
  width: 16px;
  text-align: center;
}

.dropdown-item.active i {
  color: #00d4ff;
}

@media (max-width: 768px) {
  .nav-menu {
    position: fixed;
    left: -100%;
    top: 70px;
    flex-direction: column;
    background: rgba(15, 15, 35, 0.95);
    backdrop-filter: blur(20px);
    width: 100%;
    text-align: center;
    transition: 0.3s;
    box-shadow: 0 10px 27px rgba(0, 212, 255, 0.2);
    padding: 2rem 0;
    border-top: 1px solid rgba(0, 212, 255, 0.3);
  }

  .nav-menu.active {
    left: 0;
  }

  .nav-toggle {
    display: flex;
  }
  
  .identity-switcher {
    margin-left: 0.5rem;
  }
  
  .current-identity {
    padding: 0.4rem 0.8rem;
    font-size: 0.8rem;
    min-width: 100px;
  }
  
  .dropdown-menu {
    right: -10px;
    min-width: 130px;
  }
}
</style>
